package com.mach.platform.controller;

import com.mach.platform.domain.OrderFile;
import com.mach.platform.domain.Response;
import com.mach.platform.service.OrderFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 文件操作类
 */
@RestController
@RequestMapping("order/file")
public class OrderFileCtrl extends BaseCtrl<OrderFile> {

    @Autowired
    private OrderFileServiceImpl orderFileService;

    /**
     * 分页查询
     * @param pageable
     * @param params
     * @return
     */
    @GetMapping("/findByPage")
    public Response findByPageAndRequest(@PageableDefault(size = 50) Pageable pageable, @RequestParam Map params){
        Page<OrderFile> result = orderFileService.list(pageable, params);
        return Response.ok(result);
    }

    /**
     * 文件上传
     * @param request
     * @return
     */
    @PostMapping("/batch")
    public Response handleFileUpload(HttpServletRequest request) {
        String result = orderFileService.handleFileUpload(request);
        if (result.equals("上传成功")){
            return Response.ok(result);
        }
        return Response.error(result);
    }

    /**
     * 文件下载
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/download")
    public Response download(HttpServletRequest request, HttpServletResponse response){
        orderFileService.download(request, response);
        return null;
    }

    /**
     * 文件删除
     * @param fileName
     * @param filePath
     * @return
     */
    @DeleteMapping("/delete")
    public Response deleteFile(String[] fileName, String filePath){
        int result = orderFileService.deleteFile(fileName, filePath);
        if (result > 0) {
            return Response.ok("删除成功");
        } else {
            return Response.error("删除失败");
        }
    }

    /**
     * 文件预览
     * @return
     */
    @GetMapping("/getFileFlow")
    public Response operatingConditions(HttpServletRequest request, HttpServletResponse response){
        String result = orderFileService.getFileFlow(request, response);
        if (result != null) {
            return Response.error(result);
        }
        return null;
    }


    @Override
    public Response info(Long id) {
        return null;
    }

    @Override
    public Response list(Pageable pageable, Map params) {
        return null;
    }

    @Override
    public Response delete(Long[] ids) {
        return null;
    }

    @Override
    public Response save(OrderFile orderFile) {
        return null;
    }

    @Override
    public Response update(OrderFile orderFile) {
        return null;
    }

}
